/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.fileSystem;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import tcloudclient.actionManager.ActionExecuteEventInterface;
import tcloudclient.contex.Context;
import tcloudclient.settings.XmlLoaderSaver;
import tcloudclient.watchService.WatchServiceEventInterface;

/**
 *
 * @author Tomas
 */
public class FileSystemManager implements FileSystemManagerInterface {

    private FileSystemWachtServiceListener watchServiceListener;
    private FileNode rootFolder;
    private FileSystemActionListener actionListener;
    private boolean update;

    public FileSystemManager() {        
        rootFolder = XmlLoaderSaver.loadFileSystemStatus();
        FileNodeBuilder fileNodeBuilder = new FileNodeBuilder();
        String rootFolderPath = Context.getInstance().getApplicationSettings().getRegisterDir().getAbsolutePath();
        if(rootFolder == null){
            rootFolder = fileNodeBuilder.build(rootFolderPath);
        }else{
            rootFolder = fileNodeBuilder.updateFilesSettings(new File(rootFolderPath), rootFolder);
        }
        watchServiceListener = new FileSystemWachtServiceListener(rootFolder);
        watchServiceListener.addDataChangeObserver(new FileSystemStatusDataChangeLintenerInterface() {

            @Override
            public void dataChangeEventHandler(FileNode rootFileNode) {
                update = true;
            }
        });
        actionListener = new FileSystemActionListener(rootFolder);
        Context.getInstance().setRootFolder(rootFolder);
    }

    @Override
    public WatchServiceEventInterface getObserver() {
        return watchServiceListener;
    }

    @Override
    public void uploadedChangesSuccsessFul() {
        List<FileNode> fifo = new ArrayList<>();
        fifo.add(rootFolder);
        while (!fifo.isEmpty()) {
            FileNode remove = fifo.remove(0);
            if(remove.getStatus() != FileStatusEnum.NOMODIFY){
                remove.setVersion(remove.getVersion() + 1);
                remove.setStatus(FileStatusEnum.NOMODIFY);
            }            
            Iterator<FileNode> iterator = remove.getChilds().iterator();
            while (iterator.hasNext()) {
                FileNode fileNode = iterator.next();
                if (fileNode.getStatus() != FileStatusEnum.DELETE) {
                    fifo.add(fileNode);
                } else {
                    iterator.remove();
                }
            }
        }
        Context.getInstance().getApplicationSettings().setAccHeaderVetsion(Context.getInstance().getApplicationSettings().getAccHeaderVetsion() + 1);
        XmlLoaderSaver.saveFileSystemStatus();
        XmlLoaderSaver.saveAppSettings();
        update = false;
    }
    
    @Override
    public FileNode getRootFolder() {
        return rootFolder;
    }

    @Override
    public void addDataChangeListener(FileSystemStatusDataChangeLintenerInterface listner) {
        watchServiceListener.addDataChangeObserver(listner);
    }

    @Override
    public boolean isUpdate() {
        return update;
    }

    @Override
    public void setUpdate() {
        update = true;
    }

    @Override
    public ActionExecuteEventInterface getActionListener() {
        return actionListener;
    }

    @Override
    public void removeDataChangeListener(FileSystemStatusDataChangeLintenerInterface listner) {
        watchServiceListener.removeDataChangeObserver(listner);
    }
    
    
}
